package com.ledim.fragment.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.utils.FrescoUtil;
import com.ledim.utils.LogUtil;
import org.greenrobot.eventbus.EventBus;

/**
 * Created by ccx on 16/5/20.
 * Fragment基类
 */
public abstract class LeDimBaseFragment extends Fragment implements View.OnClickListener {

    protected LeDimBaseActivity mActivity;
    protected View contentView;

    protected abstract void initContentView(View view, Bundle saveInstanceState);

    protected abstract int getLayoutId();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (contentView == null || contentView.getId() != getLayoutId()) {
            contentView = inflater.inflate(getLayoutId(), container, false);
            initContentView(contentView, savedInstanceState);
        } else {
            ViewGroup parent = (ViewGroup) contentView.getParent();
            if (parent != null) {
                parent.removeView(contentView);
            }
        }
        return contentView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mActivity = (LeDimBaseActivity) activity;
    }

    protected LeDimBaseActivity getBaseActivity() {
        return mActivity;
    }

    protected void replaceChildFragment(int contentId, Fragment newFragment,
                                        boolean addToBackStack) {
        if (newFragment != null) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(contentId, newFragment);
            if (addToBackStack) {
                transaction.addToBackStack(null);
            }
            transaction.commitAllowingStateLoss();
        }
    }

    /**
     * 直接使用，不用转换View
     *
     * @param view
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T extends View> T findViewById(View view, int id) {
        return (T) view.findViewById(id);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (useEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * @return 是否使用EventBus 默认不使用
     */
    protected boolean useEventBus() {
        return false;
    }

    @Override
    public void onStop() {
        super.onStop();
        if (useEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    public void goToOthers(Class<?> cls) {
        goToOthers(cls, null);
    }

    public void goToOthersF(Class<?> cls) {
        goToOthers(cls);
        getBaseActivity().finish();
    }

    public void goToOthers(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtra(LeDimBaseActivity.PARAM_INTENT, bundle);
        startActivity(intent);
    }

    public void goToOthersF(Class<?> cls, Bundle bundle) {
        goToOthers(cls, bundle);
        getBaseActivity().finish();
    }

    public void upToHome(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(getBaseActivity(), cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(LeDimBaseActivity.PARAM_INTENT, bundle);
        startActivity(intent);
        getBaseActivity().finish();
    }

    public void upToHome(Class<?> cls) {
        upToHome(cls, null);
    }

    public void homeAction() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// ע��
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    public void goToOthersForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent(getBaseActivity(), cls);
        intent.putExtra(LeDimBaseActivity.PARAM_INTENT, bundle);
        startActivityForResult(intent, requestCode);
    }

    public void backForResult(Class<?> cls, Bundle bundle, int resultCode) {
        Intent intent = new Intent();
        if (cls != null) {
            intent.setClass(getBaseActivity(), cls);
        }
        intent.putExtra(LeDimBaseActivity.PARAM_INTENT, bundle);
        getBaseActivity().setResult(resultCode, intent);
        getBaseActivity().finish();
    }

    @Override
    public void onClick(View v) {

    }
}
